/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaclientcp3110.DAO.SocketDAO;

import common.DAO.ProductDAO;
import common.Objects.Component;
import common.Objects.Product;
import common.xmlObjects.BaseConnectionXML;
import java.util.Vector;
import javaclientcp3110.xmlObjects.ClientXML;
import common.xmlObjects.ProductXML;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Slasher
 */
class SocketProductDAO implements ProductDAO {

    public Vector<Product> getProducts() {
        Vector<Product> products = new Vector<Product>();
        ClientXML client = new ClientXML(ClientXML.GETALL, ClientXML.PRODUCT);

        String responseXML = new SocketDAOFactory().sendXML(client.toXMLString());
        Document document = BaseConnectionXML.XMLStrTODOMDoc(responseXML);
        NodeList nodeList = document.getElementsByTagName(BaseConnectionXML.SERVER);
        if (nodeList.getLength() > 0) {
            Node node = nodeList.item(0);
            Element element = (Element) node;
            String object = element.getAttribute(BaseConnectionXML.OBJECTNAME);

            if (object.equals(BaseConnectionXML.FAILED)) {
                return null;
            }
            else {
                nodeList = node.getChildNodes();
                for (int i = 0; i < nodeList.getLength(); i++) {
                    node = nodeList.item(i);
                    products.add(new ProductXML(node).getObject());
                }
            }
        } else {
            return null;
        }
        return products;
    }

    public Product getProduct(int id) {
        ClientXML client = new ClientXML(ClientXML.GET, ClientXML.PRODUCT);
        client.addChildNode(new ProductXML(String.valueOf(id),null,null,null,null));

        Product product = new Product();
        String responseXML = new SocketDAOFactory().sendXML(client.toXMLString());
        Document document = BaseConnectionXML.XMLStrTODOMDoc(responseXML);
        NodeList nodeList = document.getElementsByTagName(BaseConnectionXML.SERVER);
        if (nodeList.getLength() >= 0) {
            Node node = nodeList.item(0);
            Element element = (Element) node;
            String object = element.getAttribute(BaseConnectionXML.OBJECTNAME);

            if (object.equals(BaseConnectionXML.FAILED)) {
                return null;
            }
            else {
                nodeList = node.getChildNodes();
                node = nodeList.item(0);
                product = new ProductXML(node).getObject();
            }
        } else {
            return null;
        }
        return product;
    }

    public boolean createProduct(Product product) {
        ClientXML client = new ClientXML(ClientXML.ADD, ClientXML.PRODUCT);
        client.addChildNode(new ProductXML(product));

        String responseXML = new SocketDAOFactory().sendXML(client.toXMLString());
        Document document = BaseConnectionXML.XMLStrTODOMDoc(responseXML);
        NodeList nodeList = document.getElementsByTagName(BaseConnectionXML.SERVER);
        if (nodeList.getLength() >= 0) {
            Node node = nodeList.item(0);
            Element element = (Element) node;
            String object = element.getAttribute(BaseConnectionXML.OBJECTNAME);

            if (object.equals(BaseConnectionXML.FAILED)) {
                return false;
            }
            return true;
        }
        return false;
    }

    public boolean deleteProduct(int id) {
        ClientXML client = new ClientXML(ClientXML.DELETE, ClientXML.PRODUCT);
        client.addChildNode(new ProductXML(String.valueOf(id),null,null,null,null));

        String responseXML = new SocketDAOFactory().sendXML(client.toXMLString());
        Document document = BaseConnectionXML.XMLStrTODOMDoc(responseXML);
        NodeList nodeList = document.getElementsByTagName(BaseConnectionXML.SERVER);
        if (nodeList.getLength() >= 0) {
            Node node = nodeList.item(0);
            Element element = (Element) node;
            String object = element.getAttribute(BaseConnectionXML.OBJECTNAME);

            if (object.equals(BaseConnectionXML.FAILED)) {
                return false;
            }
            return true;
        }
        return false;
    }

    public boolean editProduct(Product product) {
        ClientXML client = new ClientXML(ClientXML.EDIT, ClientXML.PRODUCT);
        client.addChildNode(new ProductXML(product));

        String responseXML = new SocketDAOFactory().sendXML(client.toXMLString());
        Document document = BaseConnectionXML.XMLStrTODOMDoc(responseXML);
        NodeList nodeList = document.getElementsByTagName(BaseConnectionXML.SERVER);
        if (nodeList.getLength() >= 0) {
            Node node = nodeList.item(0);
            Element element = (Element) node;
            String object = element.getAttribute(BaseConnectionXML.OBJECTNAME);

            if (object.equals(BaseConnectionXML.FAILED)) {
                return false;
            }
            return true;
        }
        return false;
    }
}